﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Localization = Galaktika.BI.Silverlight.Localization;

namespace Galaktika.BI.Silverlight.Controls
{
    using Ranet.AgOlap.Controls.ContextMenu;
    using Galaktika.BI.Runtime;

    public class RichTextBox : TextBox
    {

        public RichTextBox()
        {
            this.IsMouseWheelAttached = false;
            this.DefaultStyleKey = typeof(RichTextBox);
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.GetScroller().AttachContextMenu(p => this.ContextMenu, ContextMenuSupportMode.OnCtrlLeftMouseButtonClick);
            this.AttachContextMenu(p => this.ContextMenu, ContextMenuSupportMode.OnRightMouseButtonClick);
        }

        public ScrollViewer GetScroller()
        {
            return base.GetTemplateChild("ContentElement") as ScrollViewer;
        }

        public bool IsMouseWheelAttached { get; set; }

        protected override void OnGotFocus(RoutedEventArgs e)
        {
            base.OnGotFocus(e);
            if (!this.IsMouseWheelAttached)
            {
                this.AddMouseWheelSupport();
            }
        }

        void itemCut_ItemClick(object sender, EventArgs e)
        {
            itemCopy_ItemClick(sender, e);
            itemDelete_ItemClick(sender, e);
        }

        void itemCopy_ItemClick(object sender, EventArgs e)
        {
            this.Focus();
            Clipboard.SetClipboardText(base.SelectedText);
        }

        void itemDelete_ItemClick(object sender, EventArgs e)
        {
            this.Focus();
            base.Text = base.Text.Remove(base.SelectionStart, base.SelectionLength);
        }

        void itemPaste_ItemClick(object sender, EventArgs e)
        {
            this.Focus();
            Clipboard.SetClipboardText(base.SelectedText);
        }

        void itemSelectAll_ItemClick(object sender, EventArgs e)
        {
            this.Focus();
            base.SelectAll();
        }

        ContextMenuItem m_ItemDelete;
        ContextMenuItem m_ItemCopy;
        ContextMenuItem m_ItemCut;
        ContextMenuItem m_ItemPaste;
        protected virtual CustomContextMenu CreateContextMenu()
        {
            CustomContextMenu menu = new CustomContextMenu();
            m_ItemCut = new ContextMenuItem(Galaktika.BI.Silverlight.Localization.Cmd_Cut);
            m_ItemCut.ItemClick += new EventHandler(itemCut_ItemClick);
            menu.AddMenuItem(m_ItemCut);
            m_ItemCopy = new ContextMenuItem(Galaktika.BI.Silverlight.Localization.Cmd_Copy);
            m_ItemCopy.ItemClick += new EventHandler(itemCopy_ItemClick);
            menu.AddMenuItem(m_ItemCopy);
            m_ItemPaste = new ContextMenuItem(Galaktika.BI.Silverlight.Localization.Cmd_Paste);
            m_ItemPaste.ItemClick += new EventHandler(itemPaste_ItemClick);
            menu.AddMenuItem(m_ItemPaste);
            m_ItemDelete = new ContextMenuItem(Galaktika.BI.Silverlight.Localization.Cmd_Delete);
            m_ItemDelete.ItemClick += new EventHandler(itemDelete_ItemClick);
            menu.AddMenuItem(m_ItemDelete);
            menu.AddMenuSplitter();
            ContextMenuItem item = new ContextMenuItem(Galaktika.BI.Silverlight.Localization.Cmd_SelectAll);
            item.ItemClick += new EventHandler(itemSelectAll_ItemClick);
            menu.AddMenuItem(item);
            
            return menu;
        }

        private CustomContextMenu m_ContextMenu;
        protected CustomContextMenu ContextMenu
        {
            get
            {
                if (m_ContextMenu == null)
                {
                    m_ContextMenu = this.CreateContextMenu();
                }

                m_ItemCopy.IsEnabled = base.SelectionLength > 0;
                m_ItemDelete.IsEnabled = m_ItemCut.IsEnabled =
                    m_ItemCopy.IsEnabled && !base.IsReadOnly;
                m_ItemPaste.IsEnabled = !base.IsReadOnly && !string.IsNullOrEmpty(Clipboard.GetClipboardText());

                return m_ContextMenu;
            }
        }
    }
}
